// 递归函数
var recursion = (data, parent) => {
  return data.map((item, i, o) => {
    var loadPath = item.path.split("");
    loadPath[0] = loadPath[0].toLocaleUpperCase();
    loadPath = loadPath.join("");
    var _parent = parent ? parent : loadPath;
    // 一级路由
    var one = [
      {
        path: `${item.path}`,
        name: item.path,
        meta: {
          navName: item.authName,
          id: item.id,
        },
        component: () =>
          import(`@/views/${parent ? _parent : loadPath}/${loadPath}.vue`),
      },
    ];
    // 当前有二级，则把二级的路由和一级放在一起
    if (item.children && item.children.length) {
      one.push(recursion(item.children, _parent));
    }
    return one;
  });
};
export default (menus, $router) => {
  var routes = recursion(menus);
  routes = {
    path: "/main",
    name: "Layout",
    component: () => import("@/Layout/Layout"),
    children: [
      {
        path: "",
        component: () => import("@/views/Dashboard/Dashboard"),
      },
      {
        path: "addgoods",
        component: () => import("@/views/Goods/AddGoos"),
      },
      ...routes.flat(Infinity),
    ],
  };
  // 使用addRoutes 动态添加路由
  $router.addRoute(routes);
};
